package com.gzz.orchid.common.boot.configuraction;

import com.alibaba.fastjson2.support.spring6.data.redis.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author higmd
 */
@Slf4j
@Configuration
@Order(1)
public class RedisConfiguraction implements Ordered {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        System.out.println("配置这个RedisTemplate.......");
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        // 3.0后 默认序列化器：GenericJackson2JsonRedisSerializer
        //template.setValueSerializer(new JdkSerializationRedisSerializer());// 2.0
        template.setConnectionFactory(factory);
        // 设置key序列化方式string
        // RedisSerializer.string() 等价于 new StringRedisSerializer()
        template.setKeySerializer(RedisSerializer.string());
        // 设置value的序列化方式json，使用GenericJackson2JsonRedisSerializer替换默认序列化
        // RedisSerializer.json() 等价于 new GenericJackson2JsonRedisSerializer()
        template.setValueSerializer(RedisSerializer.json());
        // 设置hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
        //template.setConnectionFactory(factory);
        // 使配置生效
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory factory) {
        System.out.println("配置这个StringRedisTemplate.......");
        // 创建并配置 StringRedisTemplate
        StringRedisTemplate template = new StringRedisTemplate();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        //使用StringRedisSerializer来序列化和反序列化redis的key值// 设置hash key 和value序列化模式
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        //使用fastJson作为默认的序列化方式
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        template.setDefaultSerializer(genericFastJsonRedisSerializer);
        template.setValueSerializer(genericFastJsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(genericFastJsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());

        //启用事务支持
        //template.setEnableTransactionSupport(true);
        template.afterPropertiesSet();
        return template;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
